package com.sc.auction.auction.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sc.auction.auction.bean.AuctionVo;
import com.sc.auction.auction.bean.PageQuery;
import com.sc.auction.auction.bean.SuperAuctionVo;
import com.sc.auction.auction.bean.SuperSuperAuctionVo;
import com.sc.auction.auction.bean.ToppriceVo;
import com.sc.auction.auction.feign.GoodsFeignService;
import com.sc.auction.auction.feign.UserFeignService;
import com.sc.auction.auction.pojo.Atuctionbargain;
import com.sc.auction.auction.pojo.Auction;
import com.sc.auction.auction.mapper.AuctionMapper;
import com.sc.auction.auction.pojo.Auctiondetails;
import com.sc.auction.auction.pojo.Toppricee;
import com.sc.auction.auction.service.AtuctionbargainService;
import com.sc.auction.auction.service.AuctionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.auction.auction.service.AuctiondetailsService;
import com.sc.auction.auction.Lock.ToppriceeService;
import com.sc.auction.common.pojo.Goods;
import com.sc.auction.common.pojo.User;
import com.sc.auction.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pgc
 * @since 2020-09-01
 */
@Service
public class AuctionServiceImpl extends ServiceImpl<AuctionMapper, Auction> implements AuctionService {
    @Autowired
    AtuctionbargainService atuctionbargainService;
    @Autowired
    AuctiondetailsService auctiondetailsService;

    @Autowired
    GoodsFeignService goodsFeignService;
    @Autowired
    ToppriceeService toppriceeService;
    @Autowired
    UserFeignService userFeignService;


    @Override
    public R findAuctionInfoById(@RequestParam Integer auctionid) {
//        System.out.println(auctionid);
        List list=new ArrayList();
        //通过id查询拍卖品
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",auctionid);
        Auction auction=this.getOne(queryWrapper);
        //查商品细节
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("auctionId",auctionid);
        Auctiondetails auctiondetails= this.auctiondetailsService.getOne(queryWrapper1);
        //查交易记录
//        Atuctionbargain atuctionbargain=this.atuctionbargainService.getOne(queryWrapper1);
        //查询图片
        R pic=goodsFeignService.getImgById(auction.getGoodsId());
        String url= (String) pic.getData().get("imageURL");


        //查询竞价记录
        QueryWrapper queryWrapper2=new QueryWrapper();
        queryWrapper2.orderByDesc("money");
        queryWrapper2.last("limit 0,3");
        queryWrapper2.eq("auctionid",auctionid);
        List<Toppricee> top= toppriceeService.list(queryWrapper2);
        List<ToppriceVo> toppriceVos=new ArrayList<>();
        for (Toppricee t:top) {
            ToppriceVo toppriceVo=new ToppriceVo();
            toppriceVo.setAuctiontime(t.getAuctiontime());
            toppriceVo.setMoney(t.getMoney());
            toppriceVo.setBeizhu("无");
            Object g = userFeignService.getUserById(t.getUser()).getData().get("user");
            User user= JSON.toJavaObject((JSONObject)g,User.class);
            toppriceVo.setUser(user.getUsername());
            toppriceVos.add(toppriceVo);
        }


        Object g=goodsFeignService.selectGoodsById(auction.getGoodsId()).getData().get("goods");
        Goods goods=JSON.toJavaObject((JSONObject)g,Goods.class);

        SuperSuperAuctionVo superSuperAuctionVo=new SuperSuperAuctionVo();
        superSuperAuctionVo.setId(auction.getId());
        superSuperAuctionVo.setName(auction.getGoodsName());
        superSuperAuctionVo.setEndtime(auction.getEndTime());
        superSuperAuctionVo.setStarttime(auction.getStartTime());
        superSuperAuctionVo.setSecurityMoney(auction.getSecurityMoney());
        superSuperAuctionVo.setStartMoney(goods.getAssessPrice());
        superSuperAuctionVo.setStatusId(auction.getStatusId());
        superSuperAuctionVo.setTopprice(toppriceVos);
        superSuperAuctionVo.setGoodsid(auction.getGoodsId());
        superSuperAuctionVo.setUrl(url);
        return R.ok().data("auction",superSuperAuctionVo);
    }


    //查询待开拍
    @Override
    public List<AuctionVo> findAuctionSixByTime() {
   List list=new ArrayList();
        Date now=new Date();
        Page<Auction> auctionPage=new Page(1,6);
         QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("statusid",1);
   queryWrapper.gt("startTime",now);
   queryWrapper.orderByDesc("startTime");
   this.page(auctionPage,queryWrapper);
        List<Auction> Auctions=auctionPage.getRecords();
        for (Auction a:Auctions) {
            AuctionVo auctionVo=new AuctionVo();
           R goods= goodsFeignService.selectGoodsById( a.getGoodsId());
            Object g = goods.getData().get("goods");

            Goods goods1= JSON.toJavaObject((JSONObject)g,Goods.class);
            auctionVo.setId(a.getId());
            auctionVo.setName(goods1.getName());
            auctionVo.setDate(a.getStartTime());
            auctionVo.setPrice(goods1.getAssessPrice());

            R imageR = goodsFeignService.getImageByGoodsCode(goods1.getGoodsCode());
            String imageURL = (String) imageR.getData().get("imageURL");
            auctionVo.setImgUrl(imageURL);
            list.add(auctionVo);
        }
            return list;
    }

    //查询已经开拍
    @Override
    public List<AuctionVo> findAuctionSixAlreadyByTime() {
        List list=new ArrayList();
        Date now=new Date();
        Page<Auction> auctionPage=new Page(1,6);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("statusid",2);
        queryWrapper.lt("startTime",now);
        queryWrapper.orderByDesc("startTime");
        this.page(auctionPage,queryWrapper);
        List<Auction> Auctions=auctionPage.getRecords();
        for (Auction a:Auctions) {
            AuctionVo auctionVo=new AuctionVo();
            R goods= goodsFeignService.selectGoodsById( a.getGoodsId());
            Object g = goods.getData().get("goods");

            Goods goods1= JSON.toJavaObject((JSONObject)g,Goods.class);
            auctionVo.setId(a.getId());
            auctionVo.setName(goods1.getName());
            auctionVo.setDate(a.getStartTime());
            auctionVo.setPrice(goods1.getAssessPrice());

            R imageR = goodsFeignService.getImageByGoodsCode(goods1.getGoodsCode());
            String imageURL = (String) imageR.getData().get("imageURL");
            auctionVo.setImgUrl(imageURL);
            list.add(auctionVo);
        }
        return list;
    }

    //已成交六个首页展示
    @Override
    public List<AuctionVo> findBargainSixAuction() {
        List<AuctionVo> list=new ArrayList<>();
        Page<Auction> pageBean=new Page<>(1,6);
        QueryWrapper<Auction> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("statusId",3);
        this.page(pageBean, queryWrapper);
        List<Auction> Auctions = pageBean.getRecords();
        for (Auction a:Auctions) {
            AuctionVo auctionVo= new AuctionVo();
            R goods= goodsFeignService.selectGoodsById( a.getGoodsId());
            Object g = goods.getData().get("goods");

            Goods goods1= JSON.toJavaObject((JSONObject)g,Goods.class);
            auctionVo.setName(goods1.getName());
            auctionVo.setPrice(goods1.getAssessPrice());
            auctionVo.setId(a.getId());
            QueryWrapper<Atuctionbargain> queryWrapperBargain=new QueryWrapper<>();
            queryWrapperBargain.eq("auctionId",a.getId());
            Atuctionbargain byId = atuctionbargainService.getOne(queryWrapperBargain);
            auctionVo.setBargain(byId.getTradingVolume());

            R imageR = goodsFeignService.getImageByGoodsCode(goods1.getGoodsCode());
            String imageURL = (String) imageR.getData().get("imageURL");
            auctionVo.setImgUrl(imageURL);
            list.add(auctionVo);
        }

        return list;
    }

    //未开拍列表
    @Override
    public SuperAuctionVo getWaitList(long page, long limit, PageQuery pageQuery) {
        SuperAuctionVo superAuctionVo=new SuperAuctionVo();
        List list=new ArrayList();
        Date now=new Date();
        Page<Auction> auctionPage=new Page(page,limit);
        QueryWrapper queryWrapper=new QueryWrapper();
        if (pageQuery.getGoodsName()!=null&&pageQuery.getGoodsName()!=""){
            queryWrapper.like("goodsName","%"+pageQuery.getGoodsName()+"%");
        }
        if (pageQuery.getOneAndTwo()!=null&&pageQuery.getOneAndTwo()==1){
            queryWrapper.eq("statusId",1);
            queryWrapper.gt("startTime",now);
            queryWrapper.orderByDesc("startTime");
        }else if(pageQuery.getOneAndTwo()!=null&&pageQuery.getOneAndTwo()==2){
            queryWrapper.eq("statusId",2);
            queryWrapper.lt("startTime",now);
            queryWrapper.orderByDesc("startTime");
        }else if (pageQuery.getOneAndTwo()!=null&&pageQuery.getOneAndTwo()==3){
            queryWrapper.eq("statusId",pageQuery.getOneAndTwo());
            queryWrapper.orderByDesc("startTime");
        }
        if (pageQuery.getType()!=null&&pageQuery.getType()>0){
            queryWrapper.eq("goodsType",pageQuery.getType());
        }

        this.page(auctionPage,queryWrapper);
        List<Auction> Auctions=auctionPage.getRecords();

        for (Auction a:Auctions) {
            AuctionVo auctionVo=new AuctionVo();
            R goods= goodsFeignService.selectGoodsById( a.getGoodsId());
            Object g = goods.getData().get("goods");

            Goods goods1= JSON.toJavaObject((JSONObject)g,Goods.class);
            auctionVo.setName(goods1.getName());
            auctionVo.setDate(a.getStartTime());
            auctionVo.setPrice(goods1.getAssessPrice());
            auctionVo.setId(a.getId());
            R imageR = goodsFeignService.getImageByGoodsCode(goods1.getGoodsCode());
            String imageURL = (String) imageR.getData().get("imageURL");
            auctionVo.setImgUrl(imageURL);
            list.add(auctionVo);
        }
        long total = auctionPage.getTotal();
        superAuctionVo.setTotal(total);
        superAuctionVo.setAuctionVos(list);
        return  superAuctionVo;
    }

    //已经开拍列表
    @Override
    public SuperAuctionVo getOnList(long page,long limit,String goodsName) {
        return null;
    }
}
